package asynchronous;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

public class QueueTest {
    public int[] q = new int[10];
    int size = 0;
    public Thread[] getThread = new Thread[10];
    int theadSize = 0;
    ReentrantLock lock = new ReentrantLock();
    Condition isEmpty = lock.newCondition();
    Condition isFull = lock.newCondition();


    public synchronized void put(int x) throws InterruptedException {
        if(size == q.length){
//            LockSupport.park();
            isFull.await();
            return;
        }
        if(size == 0){
            //todo 喚醒
//            this.notifyAll();
            isEmpty.notifyAll();
           // LockSupport.unpark(getThread[theadSize]);
        }
            q[size++] = x;

    }
    public synchronized int get() throws InterruptedException {
        if(size == 0){
            //todo  阻塞
       // LockSupport.park();
            isEmpty.await();
     getThread[theadSize++]  =  Thread.currentThread();
        }else{
            isFull.notifyAll();
        }
        return q[--size];
    }
}
